﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.WebPages.Html;
using Model;

/// <summary>
/// Summary description for SiteHelper
/// </summary>
public class SiteHelper
{
    #region Add

    public static void AddProfile(Profile obj)
    {
        Repository rp = new Repository();

        rp.AddObject(obj.GetType().ToString().Substring(6) + "s", obj);

        rp.SaveChanges();
    }

    public static void AddQuestionType(QuestionType obj)
    {
        Repository rp = new Repository();

        rp.AddObject(obj.GetType().ToString().Substring(6) + "s", obj);

        rp.SaveChanges();
    }

    public static void AddQuestionTypeList(List<QuestionType> questionTypeList)
    {
        Repository rp = new Repository();

        foreach (QuestionType item in questionTypeList)
        {
            if (!string.IsNullOrEmpty(item.QType))
            {
                rp.AddQuestionType(item);
            }
        }

        rp.Save();
    }

    public static void AddOptionsList(List<QOption> optionList)
    {
        Repository rp = new Repository();

        foreach (QOption item in optionList)
        {
            if (!string.IsNullOrEmpty(item.Options))
            {
                rp.AddOption(item);
            }
        }

        rp.Save();
    }

    public static int AddQuestion(Question aQuestion)
    {
        Repository rp = new Repository();

        return rp.AddQuestion(aQuestion);
    }

    #endregion Add

    #region Get

    public static List<SelectListItem> GetQTypeListForDropdownList()
    {
        Repository rp = new Repository();

        List<QuestionType> allQType = rp.GetQTypeList();

        List<SelectListItem> items = new List<SelectListItem>();

        SelectListItem selectlist = null;

        foreach (var item in allQType)
        {
            selectlist = new SelectListItem();

            selectlist.Text = item.QType;
            selectlist.Value = item.QTId.ToString();

            items.Add(selectlist);
        }

        return items;
    }

    public static List<QuestionType> GetQTypeList()
    {
        Repository rp = new Repository();

        return rp.GetQTypeList();
    }

    public static QuestionType GetQuestionTypeById(int? requestId)
    {
        Repository rp = new Repository();

        return rp.GetQuestionTypeById(requestId);
    }

    public static Question GetAQuestionRandomly()
    {
        Repository rp = new Repository();

        return rp.GetAQuestionRandomly();
    }

    public static Question GetAQuestionById(int questionId)
    {
        Repository rp = new Repository();

        return rp.GetAQuestionById(questionId);
    }

    public static List<QOption> GetQOptionListByQuestionId(int questionId)
    {
        Repository rp = new Repository();

        return rp.GetQOptionsByQuestionId(questionId);
    }

    public static List<Question> GetAllQuestions()
    {
        Repository rp = new Repository();

        return rp.GetAllQuestions().ToList();
    }

    public static List<Question> GetAnsweredQuestionsAUserByuserId(int userId)
    {
        Repository rp = new Repository();

        return rp.GetAnsweredQuestionsAUserByuserId(userId).ToList();
    }

    public static int GetNumberOfQuestionAnsweredToday()
    {
        Repository rp = new Repository();

        return rp.GetNumberOfQuestionAnsweredToday();
    }

    public static Profile GetProfileByUserId(int UserId)
    {
        Repository rp = new Repository();

        return rp.GetProfileByUserId(UserId);
    }

    public static Profile GetMyProfile()
    {
        return GetProfileByUserId(SiteSecurity.GetUserId());
    }

    #endregion Get

    #region Update

    public static void UpdateQTypeByObjectAndId(QuestionType aQuestionType, int requestId)
    {
        Repository rp = new Repository();

        QuestionType editQType = rp.GetQuestionTypeById(requestId);

        editQType.QType = aQuestionType.QType;
        editQType.CreateDate = aQuestionType.CreateDate;

        rp.Save();
    }

    public static void UpdateQuestionByIdAndObject(Question aQuestion)
    {
        Repository rp = new Repository();

        rp.UpdateQuestionByIdAndObject(aQuestion);
    }

    public static void UpdateQuestionByIdAndObjectNoTimeLimit(Question aQuestion)
    {
        Repository rp = new Repository();

        rp.UpdateQuestionByIdAndObjectNoTimeLimit(aQuestion);
    }

    #endregion Update

    #region Delete

    public static bool DeleteQuestionTypeById(int requestId)
    {
        Repository rp = new Repository();
        try
        {
            rp.DeleteQuestionTypeById(requestId);
        }
        catch (Exception ex)
        {
            return false;
        }
        return true;
    }

    public static bool DeleteQuestionById(int requestId)
    {
        Repository rp = new Repository();

        try
        {
            rp.DeleteQuestionById(requestId);
        }
        catch (Exception ex)
        {
            return false;
        }
        return true;
    }

    #endregion Delete

    #region misc

    public static void ReloadAllQuestion()
    {
        Repository rp = new Repository();

        rp.ReloadAllQuestion();
    }

    public static void Save()
    {
        Repository rp = new Repository();

        rp.Save();
    }

    public static bool CheckAnswer(List<QOption> postedOptionList, List<QOption> answerOptionList)
    {
        int count = 0;

        foreach (QOption pOption in postedOptionList)
        {
            QOption aOption = answerOptionList.FirstOrDefault(x => x.Options == pOption.Options && x.IsAnswer == true);

            if (aOption != null)
            {
                count++;
            }
        }

        if (count > 0 && count == answerOptionList.Count && postedOptionList.Count == answerOptionList.Count)
        {
            return true;
        }

        return false;
    }

    #endregion misc

    public static string GetStatus(int? IsAnswered)
    {
        string status = string.Empty;

        if (IsAnswered.HasValue)
        {
            if (IsAnswered == 0)
                status = Question.NotAnswered;

            if (IsAnswered == 1)
                status = Question.CantAnswered;

            if (IsAnswered == 2)
                status = Question.RightAnswered;

            if (IsAnswered == 3)
                status = Question.WrongAnswered;
        }

        return status;
    }
}